<?php

namespace App\Common;


use App\Model\t_s_accsuper_config;
use App\X\XContext;
use App\X\XDataBase;

class SysContext extends XContext
{
    /**
     * X
     * @return string
     * @throws \Exception
     */
    public function funcGetX()
    {
        return (string)$this->get('x');
    }

    /**
     * set X
     * @param $x
     */
    public function funcSetX($x)
    {
        $this->set('x', $x);
    }


    /**
     * db
     * @return \App\Common\DB\CommonMysql
     * @throws \Exception
     */
    public function db()
    {
        $db = $this->get(\App\Common\DB\Pool\PoolMysql::class);
        if ($db === null)
        {
            $app_db = new XDataBase(\App\Common\DB\Pool\PoolMysql::class);

            $db = $app_db->funcGetDB();
        }


        return $db;
    }

    /**
     * @return \App\Common\DB\CommonRedis
     * @throws \Exception
     */
    public function redis()
    {
        $db = $this->get(\App\Common\DB\Pool\PoolRedis::class);
        if ($db === null)
        {
            $app_db = new XDataBase(\App\Common\DB\Pool\PoolRedis::class);

            /** @var \App\Common\DB\CommonRedis $db */
            $db = $app_db->funcGetDB();
            $db->select(0);
        }


        return $db;
    }


    /**
     * get config
     *
     * @return t_s_accsuper_config|null
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function funcGetX__config()
    {
        $key = 'funcGetX__config';

        $entity = $this->get($key);
        if ($entity == null)
        {
            $entity = t_s_accsuper_config::funcLoad($this->db(), $this->funcGetX());
            $this->set($key, $entity);
        }


        return $entity;
    }

    /**
     * set config
     * @param $entity
     */
    public function funcSetX__config($entity)
    {
        $this->set('funcGetX__config', $entity);
    }


    public function funcGetWebIP()
    {
        return $this->get('ip');
    }
}